package com.ja.openchat.viewmodels;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.google.gson.JsonObject;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.ui.found.CircleHomeActivity;
import com.ja.openchat.ui.me.ExpressionStoreFragment;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.discover.DiscoverMenuInfo;
import com.zy.openchat.core.model.discover.DiscoverMenuSectionInfo;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.tatarka.bindingcollectionadapter2.ItemBinding;


public class FoundViewModel extends BaseViewModel {

    public SingleLiveEvent<Integer> mCircleUnreadCount = new SingleLiveEvent<>();
    public ObservableList<FoundItemViewModel> observableList = new ObservableArrayList<>();

    public FoundViewModel(@NonNull Application application) {
        super(application);
    }

    //朋友圈的点击事件
    public BindingCommand circleOnClickCommand = new BindingCommand(() -> {
        startActivity(CircleHomeActivity.class);
//        startContainerActivity(BasePagerGoChatCircleFragment.class.getCanonicalName());
    });
    //表情商店的点击事件
    public BindingCommand emojiOnClickCommand = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putInt("type", 1);
        startContainerActivity(ExpressionStoreFragment.class.getCanonicalName(), bundle);
    });

    public void requsetData() {
        //请求数据
        TelegramManager.getTelegramManager().queryDiscoverSections(getApplication(), new TgResultBlock<List<DiscoverMenuSectionInfo>>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, List<DiscoverMenuSectionInfo> obj) {
                if (obj != null) {
                    DiscoverMenuSectionInfo.saveDiscoverMenus(obj);
                    observableList.clear();
                    for (DiscoverMenuSectionInfo sc : obj) {
                        if (sc.menus != null && sc.menus.size() > 0) {
                            for (DiscoverMenuInfo menu : sc.menus) {
                                observableList.add(new FoundItemViewModel(FoundViewModel.this, menu));
                            }
                        }
                    }
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
            }
        });
        TelegramManager.getTelegramManager().getCircleUnreadCount();
    }

    @Override
    public void onResume() {
        super.onResume();
        requsetData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(TgEvent tg_event) {
        if (tg_event == null) {
            return;
        }
        switch (tg_event.event) {
            case EUser_Td_update_Circle_Unread_count:
                mCircleUnreadCount.setValue(TelegramManager.getTelegramManager().mCircleUnreadCount);
                break;
        }
    }

    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of((itemBinding, position, item) -> itemBinding.set(BR.viewModel, R.layout.item_multi_found));

}
